←Select platform

ReadTagsWithOffsets(Stream,int,out long[]) Method

Summary
Reads all the tags stored in a TIFF file, along with the offsets for each tag.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public IList<RasterTagMetadata> ReadTagsWithOffsets( 
   Stream stream, 
   int pageNumber, 
   out long[] offsets 
) 
- (nullable NSArray<LTRasterTagMetadata *> *)readTagsFromStream:(LTLeadStream *)stream  
                                                     pageNumber:(NSInteger)pageNumber  
                                                        offsets:(NSMutableArray<NSNumber *> *)offsets  
                                                          error:(NSError **)error 
public RasterCollection<RasterTagMetadata> readTagsWithOffsets(ILeadStream stream, int pageNumber, long[][] offsets) 
public: 
IList<RasterTagMetadata^>^ ReadTagsWithOffsets(  
   Stream^ stream, 
   int pageNumber, 
   [Out] array<int64> offsets 
)  
def ReadTagsWithOffsets(self,stream,pageNumber,offsets): 

Parameters

stream
A Stream containing the input image data.

pageNumber
1-based index of the page from which to read the tags.

offsets
An array that contains the offsets for each tag.

Return Value

A collection of RasterTagMetadata containing all the tags found in the file. If the file does not contain any tags, an empty collection will be returned. If the file format does not support tags, an exception will be thrown.

Remarks

To read a specific tag stored in a file, use ReadTag and to enumerate all the tag ids (but not the data) stored in a file use EnumTags.

This method will throw an exception if the file format does not support tags. To determine whether a file format supports tags, use TagsSupported. You can also automatically load all the tags stored in a file during a load operation by setting the CodecsLoadOptions.Tags property to true. The tags data will be stored in the resulting image RasterImage.Tags collection.

To load all the tags stored in a disk file, use ReadTags.

Example

This example will load all the tags, with their offsets, from a file.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
public void ReadTagsWithOffsetsExample() 
{ 
   // Prompt the user for an image file 
   string imageFileName = PromptForFileName(); 
 
   // Initialize LEADTOOLS 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      // Get the file format 
      RasterImageFormat format; 
 
      using (CodecsImageInfo info = codecs.GetInformation(imageFileName, false)) 
      { 
         format = info.Format; 
      } 
 
      // Load the tags, with their offsets 
      IList<RasterTagMetadata> tags = null; 
      long[] offsets = null; 
      if (RasterCodecs.TagsSupported(format)) 
         tags = codecs.ReadTagsWithOffsets(imageFileName, 1, out offsets); 
 
      string txtFileName = Path.Combine( 
         Path.GetDirectoryName(imageFileName), 
         Path.GetFileNameWithoutExtension(imageFileName) + "_metadata.txt"); 
 
      using (StreamWriter writer = File.CreateText(txtFileName)) 
      { 
         // Write the tags 
         WriteTags(writer, "Tags", tags, offsets); 
      } 
 
      // Show the text file we created 
      System.Diagnostics.Process.Start(txtFileName); 
   } 
} 
 
private static void WriteTags(StreamWriter writer, string name, IList<RasterTagMetadata> tags, long[] offsets) 
{ 
   writer.WriteLine("{0}:", name); 
 
   if (tags != null) 
   { 
      int x = 0; 
      foreach (RasterTagMetadata tag in tags) 
      { 
         writer.WriteLine("Id: 0x{0}, offset: {1}", tag.Id.ToString("X"), offsets[x]); 
         x++; 
      } 
   } 
   else 
   { 
      writer.WriteLine("Not supported"); 
   } 
 
   writer.WriteLine(); 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
public void readTagsWithOffsetsExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String imageFileName = combine(LEAD_VARS_IMAGES_DIR, "ocr1-4.tif"); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // Get the file format 
   RasterImageFormat format; 
   CodecsImageInfo info = codecs.getInformation(imageFileName, false); 
   format = info.getFormat(); 
 
   // Load the tags, with their offsets 
   List<RasterTagMetadata> tags = null; 
   long[][] offsets = new long[1000][1000]; 
   if (RasterCodecs.tagsSupported(format)) { 
      ILeadStream imageFileStream = LeadStreamFactory.create(imageFileName); 
      tags = codecs.readTagsWithOffsets(imageFileStream, 1, offsets); 
   } 
   File imageFile = new File(imageFileName); 
   String imageFileWithoutExtension = imageFileName.substring(0, imageFileName.indexOf(".")); 
   String txtFileName = imageFileWithoutExtension + "_metadata.txt"; 
   FileWriter writer = new FileWriter(txtFileName); 
 
   // Write the tags 
   writeTags(writer, "Tags", tags, offsets); 
 
   assertTrue("File unsuccessfully created", imageFile.exists()); 
} 
 
private static void writeTags(FileWriter writer, String name, List<RasterTagMetadata> tags, long[][] offsets) 
      throws IOException { 
 
   writer.write(name + ":\n"); 
 
   if (tags != null) { 
      int x = 0; 
      for (RasterTagMetadata tag : tags) { 
         writer.write(String.format("Id: 0x%s, offset: %s%n", tag.getId(), offsets[x])); 
         x++; 
      } 
   } else { 
      writer.write("Not supported\n"); 
   } 
   writer.write("\n"); 
 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.